package com.fr.report.script.function;

import com.fr.base.FRContext;
import com.fr.base.core.DateUtils;
import com.fr.base.core.list.IntList;
import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;
import com.fr.report.script.core.FArray;
import com.fr.util.Utils;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RANGE extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    if (paramArrayOfObject.length < 1)
      return Primitive.ERROR_NAME;
    if (paramArrayOfObject[0] instanceof Date)
      return rangeDate(paramArrayOfObject);
    return rangeNumber(paramArrayOfObject);
  }

  private Object rangeNumber(Object[] paramArrayOfObject)
  {
    int i = 1;
    int j = 1;
    int k = 1;
    try
    {
      if (paramArrayOfObject.length == 1)
      {
        j = Utils.string2Number(paramArrayOfObject[0].toString()).intValue() + 1;
      }
      else if (paramArrayOfObject.length >= 2)
      {
        if (paramArrayOfObject.length > 2)
          k = Utils.string2Number(paramArrayOfObject[2].toString()).intValue();
        i = Utils.string2Number(paramArrayOfObject[0].toString()).intValue();
        j = Utils.string2Number(paramArrayOfObject[1].toString()).intValue() + ((k > 0) ? 1 : -1);
      }
    }
    catch (NumberFormatException localNumberFormatException)
    {
      FRContext.getLogger().log(Level.WARNING, localNumberFormatException.getMessage(), localNumberFormatException);
      return Primitive.ERROR_VALUE;
    }
    if (k == 0)
      return Primitive.ERROR_VALUE;
    int[] arrayOfInt = IntList.range(i, j, k);
    Integer[] arrayOfInteger = new Integer[arrayOfInt.length];
    for (int l = 0; l < arrayOfInt.length; ++l)
      arrayOfInteger[l] = new Integer(arrayOfInt[l]);
    return new FArray(arrayOfInteger);
  }

  private Object rangeDate(Object[] paramArrayOfObject)
  {
    if (paramArrayOfObject.length < 1)
      return Primitive.ERROR_NAME;
    Date localDate1 = DateUtils.createDate(2009, 10, 23);
    Date localDate2 = DateUtils.createDate(2009, 10, 23);
    int i = 1;
    try
    {
      if (paramArrayOfObject.length == 1)
      {
        Date[] arrayOfDate1 = new Date[1];
        arrayOfDate1[0] = ((Date)paramArrayOfObject[0]);
        return new FArray(arrayOfDate1);
      }
      if (paramArrayOfObject.length >= 2)
      {
        if (paramArrayOfObject.length > 2)
          i = Utils.string2Number(paramArrayOfObject[2].toString()).intValue();
        localDate1 = (Date)paramArrayOfObject[0];
        localDate2 = (Date)paramArrayOfObject[1];
      }
    }
    catch (NumberFormatException localNumberFormatException)
    {
      FRContext.getLogger().log(Level.WARNING, localNumberFormatException.getMessage(), localNumberFormatException);
      return Primitive.ERROR_VALUE;
    }
    if (i == 0)
      return Primitive.ERROR_VALUE;
    int j = (int)DateUtils.subtractDate(localDate2, localDate1, "d");
    int[] arrayOfInt = IntList.range(0, j + ((i > 0) ? 1 : -1), i);
    Date[] arrayOfDate2 = new Date[arrayOfInt.length];
    for (int k = 0; k < arrayOfInt.length; ++k)
      arrayOfDate2[k] = DateUtils.datePlusInteger(localDate1, arrayOfInt[k]);
    return new FArray(arrayOfDate2);
  }

  public Function.Type getType()
  {
    return Function.ARRAY;
  }

  public String getCN()
  {
    return "RANGE(from，to，step)函数表示从整数from开始，以step为每一步的大小，直到整数to的一个数字序列。\n例如：\nRANGE(1,5,1)表示从1开始，直到5(包括5)，每一步大小为1，那么它返回一个数字序列为[1,2,3,4,5]。\nRANGE(-1,6,2)表示从-1开始，直到6(包括6)，每一步大小为2，那么它返回一个数字序列为[-1,1,3,5]。\n备注：RANGE函数有三种参数形式：\n1 RANGE(to)，默认的from为1，step为1，例如：\n  RANGE(4)返回[1,2,3,4]。\n  RANGE(-5)返回[]。\n2 RANGE(from,to)，默认的step为1，例如:\n  RANGE(-1,3)返回[-1,0,1,2,3]。\n  RANGE(0,5)返回[0,1,2,3,4,5]。\n3 RANGE(from,to,step)，三个参数的情况参照上面的注释，例如：\n  RANGE(6,-1,-2)返回[6,4,2,0]。\n  RANGE(4,1,1)返回[]。";
  }

  public String getEN()
  {
    return "RANGE(from,to,step)returns a serial of numbers from \"from\" to \"to\" and the step is \"step\".For example:\nRANGE(1,5,1)from 1,to 5,and step is1,it returns[1,2,3,4,5].\nRANGE(-1,6,2)from -1,to 6, and step is 2,it returns[-1,1,3,5].\n\nBTW:RANGE function has three parameter formats:\n1 RANGE(to) the default from is 0,step is 1, e.g:\n  RANGE(4)returns[1,2,3,4].\n  RANGE(-5)returns[]\n2 RANGE(from,to) the default step is 1, e.g:\n  RANGE(-1,3)returns[-1,0,1,2,3].\n  RANGE(0,5)returns[0,1,2,3,4,5].\n3 RANGE(from,to,step), e.g:\n  RANGE(6,-1,-2)returns[6,4,2,0].\n  RANGE(4,1,1)returns[].";
  }
}